



package jnpf.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.entity.DocumentVersionEntity;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.model.documentversion.DocumentVersionForm;
import jnpf.model.documentversion.DocumentVersionInfoVO;
import jnpf.model.documentversion.DocumentVersionListVO;
import jnpf.model.documentversion.DocumentVersionPagination;
import jnpf.permission.entity.UserEntity;
import jnpf.permission.service.UserService;
import jnpf.service.DocumentVersionService;
import jnpf.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 *
 * DocumentVersion
 * @版本： V3.2.0
 * @版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2023-12-20
 */
@Slf4j
@RestController
@Tag(name = "DocumentVersion" , description = "example")
@RequestMapping("/api/example/DocumentVersion")
public class DocumentVersionController {


    @Autowired
    private UserService userService;
    @Autowired
    private UserProvider userProvider;

    @Autowired
    private DocumentVersionService documentVersionService;



    /**
     * 列表
     *
     * @param documentVersionPagination
     * @return
     */
    @Operation(summary = "获取列表")
	@PostMapping("/getList")
    public ActionResult list(@RequestBody DocumentVersionPagination documentVersionPagination)throws IOException, WorkFlowException{
        //树形判断是否有参数，有参数不造树
        List<DocumentVersionEntity> list= documentVersionService.getList(documentVersionPagination);
        //处理id字段转名称，若无需转或者为空可删除
    List<DocumentVersionListVO> listVO=JsonUtil.getJsonToList(list,DocumentVersionListVO.class);
        UserInfo userInfo = userProvider.get();
    for(DocumentVersionListVO documentVersionVO:listVO){
        ActionResult info = info(documentVersionVO.getId());
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
        documentVersionVO.setDocid_name(documentVersionVO.getDocid());

        documentVersionVO.setDescription_name(documentVersionVO.getDescription());

        documentVersionVO.setEnabledmark_name(documentVersionVO.getEnabledmark());

        documentVersionVO.setCreatortime_name(documentVersionVO.getCreatortime());

        documentVersionVO.setCreatoruserid_name(userService.getById(documentVersionVO.getCreatoruserid()).getRealName());

        documentVersionVO.setLastmodifytime_name(documentVersionVO.getLastmodifytime());

        documentVersionVO.setLastmodifyuserid_name(userService.getById(documentVersionVO.getLastmodifyuserid()).getRealName());

        documentVersionVO.setMainurl_name(documentVersionVO.getMainurl());

        documentVersionVO.setCopyurl_name(documentVersionVO.getCopyurl());

        documentVersionVO.setVersion_name(documentVersionVO.getVersion());

		//子表数据转换
     }

//		for(DocumentVersionListVO vo:listVO){
//            FlowTaskEntity flowTaskEntity = generaterSwapUtil.getInfoSubmit(vo.getId(), FlowTaskEntity::getStatus);
//            if (flowTaskEntity!=null){
//                vo.setFlowState(flowTaskEntity.getStatus());
//            }else{
//                vo.setFlowState(null);
//            }
//		}
         PageListVO vo=new PageListVO();
         vo.setList(listVO);
         PaginationVO page=JsonUtil.getJsonToBean(documentVersionPagination,PaginationVO.class);
         vo.setPagination(page);
         return ActionResult.success(vo);

    }


    /**
     * 创建
     *
     * @param crData
     * @return
     */
    @PostMapping("/{id}")
    @Transactional
    @Operation(summary = "创建")
    public ActionResult create(@PathVariable("id") String id,@RequestBody  Map<String,Object> crData) throws DataException {
        DocumentVersionForm documentVersionForm = JsonUtil.getJsonToBean(crData,DocumentVersionForm.class);
	    String b =  documentVersionService.checkForm(documentVersionForm,0);
        if (StringUtil.isNotEmpty(b)){
            return ActionResult.fail(b + "不能重复");
        }
        String mainId =id;

        UserInfo userInfo=userProvider.get();
//		UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
//        GeneraterSwapUtil.swapDatetime(documentVersionForm);
        documentVersionForm.setCreatortime(DateUtil.getNow());
        documentVersionForm.setCreatoruserid(userInfo.getUserId());
        DocumentVersionEntity entity = JsonUtil.getJsonToBean(documentVersionForm, DocumentVersionEntity.class);
        entity.setId(mainId);
        documentVersionService.save(entity);

        return ActionResult.success("保存成功");
}

    /**
    * 信息
    *
    * @param id
    * @return
    */
    @Operation(summary = "信息")
    @GetMapping("/{id}")
    public ActionResult<DocumentVersionInfoVO> info(@PathVariable("id") String id){
        DocumentVersionEntity entity= documentVersionService.getInfo(id);
        DocumentVersionInfoVO vo=JsonUtil.getJsonToBean(entity, DocumentVersionInfoVO.class);
        if(vo==null){ return ActionResult.success(vo);}
        if(vo.getCreatortime()!=null){
            vo.setCreatortime(vo.getCreatortime());
        }
        UserEntity user = userService.getById(vo.getCreatoruserid());
        vo.setCreatoruserid(user.getRealName());
        if(vo.getLastmodifytime()!=null){
            vo.setLastmodifytime(vo.getLastmodifytime());
        }
        user = userService.getById(vo.getLastmodifyuserid());
         vo.setLastmodifyuserid(user.getRealName());

		//子表
		//副表
        return ActionResult.success(vo);
    }





   /**
    * 更新
    *
    * @param id
    * @param upData
    * @return
    */
    @PutMapping("/{id}")
    @Transactional
    @Operation(summary = "更新")
    public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid Map<String,Object> upData) throws DataException {
        DocumentVersionForm documentVersionForm = JsonUtil.getJsonToBean(upData,DocumentVersionForm.class);
	    String b = documentVersionService.checkForm(documentVersionForm,1);
        if (StringUtil.isNotEmpty(b)){
            return ActionResult.fail(b + "不能重复");
        }
        UserInfo userInfo=userProvider.get();
        DocumentVersionEntity entity= documentVersionService.getInfo(id);
        if(entity!=null){
                documentVersionForm.setLastmodifytime(DateUtil.getNow());
                documentVersionForm.setLastmodifyuserid(userInfo.getUserId());
                DocumentVersionEntity subentity=JsonUtil.getJsonToBean(documentVersionForm, DocumentVersionEntity.class);
                subentity.setCreatortime(entity.getCreatortime());
                subentity.setCreatoruserid(entity.getCreatoruserid());
				boolean b1 = documentVersionService.updateById(subentity);
				if (!b1){
				return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
				}
            return ActionResult.success("更新成功");
        }else{
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

   /**
    * 删除
    *
    * @param id
    * @return
    */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id){
        DocumentVersionEntity entity= documentVersionService.getInfo(id);
        if(entity!=null){
            documentVersionService.delete(entity);
        }
        return ActionResult.success("删除成功");
    }





}
